<?php

namespace Trawind\Events\Subscribers\Finance\Credit;


use Trawind\Events\Events\Purchase\ClosePurchaseEvent;
use Trawind\Events\Events\Purchase\ClosePurchaseReturnEvent;
use Trawind\Extension\Models\Deploy\BusinessType;

class CreditSubscriber
{
    private $businessType = \Trawind\Basics\Enums\BusinessType::FINANCE_VENDOR_CREDIT;

    public function closePurchase($event)
    {
           $repository = BusinessType::getEloquentById($this->businessType);
            $repository->callback($event->payload,ClosePurchaseEvent::class);
    }

    public function closePurchaseReturn($event)
    {
        $repository = BusinessType::getEloquentById($this->businessType);
        $repository->callback($event->payload,ClosePurchaseEvent::class);
    }

    /**
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            ClosePurchaseEvent::class,
            static::class . '@closePurchase'
        );
        $events->listen(
            ClosePurchaseReturnEvent::class,
            static::class . '@closePurchaseReturn'
        );
    }
}
